package org.lib.redishello.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Redis配置
 * @author ben
 * @date 2021-08-23 14:59:55 CST
 */
@Configuration
public class RedisConfig {

	/**
	 * RedisTemplate配置：序列化器
	 * @author ben
	 * @date 2021-08-23 15:01:41 CST
	 * @param redisConnectionFactory
	 * @return
	 */
	@Bean
	public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
		// 1.创建模板
		RedisTemplate template = new RedisTemplate();
		// 2.关联redisConnectionFactory
		template.setConnectionFactory(redisConnectionFactory);
		// 3.创建序列化器对象
		GenericToStringSerializer serializer = new GenericToStringSerializer(Object.class);
		
		// 4.设置 value、key 的转化格式——序列化器
		template.setValueSerializer(serializer);
		template.setKeySerializer(new StringRedisSerializer());
		
		template.afterPropertiesSet();
		
		return template;
	}
	
}
